package 华为OD机试真题2023.基础题;

import java.io.File;
import java.io.FileNotFoundException;
import java.util.*;

public class HJ06_打印文件 {
    public static void main(String[] args) throws FileNotFoundException {
        Scanner in = new Scanner(new File("src/main/java/华为OD机试真题2023/基础题/input/06.txt"));
        int N = in.nextInt();
        in.nextLine(); //吸收换行
        List<Queue<Node>> queueList = new ArrayList<Queue<Node>>();
        for (int i = 0; i < 5; i++) {
            Queue<Node> q = new PriorityQueue<Node>(new Comparator<Node>() {
                @Override
                public int compare(Node o1, Node o2) {
                    return o2.getWeight() - o1.getWeight();
                }
            });
            queueList.add(q);
        }
        int cnt = 1;
        while (N-- > 0) {
            String[] sArray = in.nextLine().split(" ");
            String type = sArray[0];
            if ("IN".equals(type)) {
                int num = Integer.parseInt(sArray[1]);
                int weight = Integer.parseInt(sArray[2]);
                queueList.get(num - 1).add(new Node(cnt++, weight));
            } else if ("OUT".equals(type)) {
                int num = Integer.parseInt(sArray[1]);
                System.out.println(queueList.get(num - 1).poll().getCount());
            }
        }
    }

    static class Node {
        private int count;
        private int weight;

        public Node(int count, int weight) {
            this.count = count;
            this.weight = weight;
        }

        public int getCount() {
            return count;
        }

        public void setCount(int count) {
            this.count = count;
        }

        public int getWeight() {
            return weight;
        }

        public void setWeight(int weight) {
            this.weight = weight;
        }
    }
}

